package cn.rongdao.jrkabao.widget;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.mifengkong.frtools.util.FRDisplayMetrics;


/**
 * Created by jack on 17/5/19
 */

public class CustomWelcomeView extends View {

    private int topColor = Color.parseColor("#16ffffff");
    private int middleColor = Color.parseColor("#32ffffff");
    private int bottomColor = Color.parseColor("#48ffffff");

    private Paint paint;

    private float circleCenter;
    private float topCircleCenter;
    private float middleCircleCenter;
    private float bottomCircleCenter;

    private float radiusX;

    private boolean isInit = false;

    public CustomWelcomeView(Context context) {
        this(context, null);
    }

    public CustomWelcomeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomWelcomeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isInit) {
            circleCenter = getMeasuredHeight() + getMeasuredWidth() * 3 / 4;
            topCircleCenter = getMeasuredHeight() + getMeasuredWidth() * 3 / 4;
            middleCircleCenter = getMeasuredHeight() + getMeasuredWidth() * 3 / 4;
            bottomCircleCenter = getMeasuredHeight() + getMeasuredWidth() * 3 / 4;
            radiusX = getMeasuredWidth() * 3 / 4;
            isInit = true;
        }
        paint.setColor(topColor);
        canvas.drawCircle(getMeasuredWidth() / 2, topCircleCenter, radiusX, paint);
        paint.setColor(middleColor);
        canvas.drawCircle(getMeasuredWidth() / 2, middleCircleCenter, radiusX, paint);
        paint.setColor(bottomColor);
        canvas.drawCircle(getMeasuredWidth() / 2, bottomCircleCenter, radiusX, paint);
    }

    public void startAnim() {
        ValueAnimator topAnim = ValueAnimator.ofFloat(0, FRDisplayMetrics.dp2px(getContext(), 190.0f));
        topAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                topCircleCenter = circleCenter - (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        topAnim.setStartDelay(100);
        topAnim.setDuration(800);
        topAnim.start();

        ValueAnimator middleAnim = ValueAnimator.ofFloat(0, FRDisplayMetrics.dp2px(getContext(), 150.0f));
        middleAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                middleCircleCenter = circleCenter - (float) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        middleAnim.setStartDelay(300);
        middleAnim.setDuration(600);
        middleAnim.start();

        ValueAnimator bottomAnim = ValueAnimator.ofFloat(0, FRDisplayMetrics.dp2px(getContext(), 105.0f));
        bottomAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                bottomCircleCenter = circleCenter - (float) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        bottomAnim.setStartDelay(500);
        bottomAnim.setDuration(400);
        bottomAnim.start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width;
        int height;
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);

        if (mode == MeasureSpec.EXACTLY) {
            width = size;
        } else {
            width = FRDisplayMetrics.getScreenWidth((Activity) getContext());
        }

        mode = MeasureSpec.getMode(heightMeasureSpec);
        size = MeasureSpec.getSize(heightMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            height = size;
        } else {
            height = FRDisplayMetrics.getScreenHeight((Activity) getContext());
        }
        setMeasuredDimension(width, height);
    }
}